import request from "@/utils/request";

//获取全部公告
export type Common<T> = {
    code:number
    data:T
    message:string
}
export type NoticeItem = {
    id: number
    img:string
    name: string
    content: string
    time: string
}

export const getNotice = () =>{
    return request<Common<NoticeItem[]>>({
        method:"GET",
        url:"/notice/getAll",
    })
}

//增加公告
type AddInfo = {
    title:string,
    author:string,
    publishTime:string,
    status:string,
}
type AddResult = {
    code:number
    message: string
    data: string
}

export const addNotice = (form: AddInfo) =>{
    return request<AddResult>({
        method:"POST",
        url:"/admin/add",
        data: JSON.stringify({
            title:form.title,
            author:form.author,
            status:form.status,
                }),
        headers: {
            'Content-Type': 'application/json'
        }
    })
}

//编辑公告
type EditInfo = {
  id:number,
  title: string,
  author: string,
  publishTime: string,
  status: string,
}
export const editNotice = (form: EditInfo) =>{
    return request<AddResult>({
        method:"POST",
        url:"/admin/edit",
        data: JSON.stringify({
            id:form.id,
            title:form.title,
            author:form.author,
            status:form.status,
                }),
        headers: {
            'Content-Type': 'application/json'
        }
    })
}

//单行删除菜单
export const delFrom = (id: number) => { 
    return request<Common<boolean>>({
        method: 'DELETE',
        url: `/notice/delete?id=${id}`
    });
}
//批量删除菜单
export const batchDelFrom = (ids:number[]) => {
    return request<Common<boolean>>({
        method:'DELETE',
        url:`/notice/delete/batch`,
        data: ids
        
    })
}

//分页接口
export type PageInfo = {
    pageNum:number, 
    pageSize:number,
    search:string,
}
export type PageResult = {
    current:number
    pages:number
    records:NoticeItem []
    size:number
    total:number
}
export const paginationInquire = (page:PageInfo) =>{
    return request<Common<PageResult>>({
        method:"GET",
        url:"/notice/page",
        params: page,
    })
}

